package com.seabook.store.goodmgr.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

import com.seabook.store.goodmgr.domain.Category;

public class CategoryDaoImpl extends JdbcDaoSupport implements CategoryDao {

    private final String SQL_INSERT_CATEGORY = "insert into Category(id, name, categoryId, corder) values (?,?,?,?)";
    private final String SQL_UPDATE_CATEGORY = "update Category set name=? , categoryId=? , corder = ? where id = ?";
    private final String SQL_GENERATE_CATEGORYID = "select max(id)+1 from Category";
    private final String SQL_DELETE_CATEGORY = "delete from Category where id = ? and id not in (select distinct categoryId from Category union select distinct categoryId from Product)";

    private CategoryRowMapper mapper = new CategoryRowMapper();

    public List<Category> getSubCategories(int categoryId) {
        // TODO Auto-generated method stub
        return this.getJdbcTemplate().query(
                "select * from Category where categoryId = ? order by corder ",
                new Object[] { categoryId }, mapper);
    }

    public Category getCategory(int categoryId) {
        // TODO Auto-generated method stub
        return this.getJdbcTemplate().queryForObject(
                "select * from Category where id = ?  order by corder",
                new Object[] { categoryId }, mapper);
    }

    public void saveCategory(Category category) {
        if (category.getId() == 0) {
            insertCategory(category);
        } else {
            updateCategory(category);
        }
    }

    private void updateCategory(Category category) {
        this.getJdbcTemplate().update(
                SQL_UPDATE_CATEGORY,
                new Object[] { category.getName(), category.getCategoryId(),
                        category.getOrder(), category.getId() });

    }

    private synchronized void insertCategory(Category category) {
        // TODO Auto-generated method stub
        int id = this.getJdbcTemplate().queryForInt(SQL_GENERATE_CATEGORYID);
        if(id == 0) id =1;
        this.getJdbcTemplate().update(
                SQL_INSERT_CATEGORY,
                new Object[] { id, category.getName(),
                        category.getCategoryId(), category.getOrder() });

    }

    class CategoryRowMapper implements RowMapper<Category> {
        public Category mapRow(ResultSet rs, int rowNumber) throws SQLException {
            Category category = new Category();
            category.setId(rs.getInt("id"));
            category.setName(rs.getString("name"));
            category.setCategoryId(rs.getInt("categoryId"));
            category.setOrder(rs.getInt("corder"));
            return category;
        }
    }

    public int deleteCategory(int id) {
        return getJdbcTemplate().update(SQL_DELETE_CATEGORY,
                new Object[] { id });
    }
}
